package modele;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

/**
 *
 * @author mmirtil
 */
public class DAO {

    private static final String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";

       public static List<String> getLesDeps() {
        List<String> lesDeps = new ArrayList<String>();
        
        try {
            URL myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());
            
            Element racine = doc.getDocumentElement();
            NodeList listeDep = racine.getElementsByTagName("Departement");
            //recup des deps
            for (int i = 0; i < listeDep.getLength(); i++) {
                Node dep = listeDep.item(i);
                NodeList lesProprietes = dep.getChildNodes();
                //recherche du num
                for (int j = 0; j < lesProprietes.getLength(); j++) {
                    if (lesProprietes.item(j).getNodeName().equals("num")) {
                        lesDeps.add(lesProprietes.item(j).getTextContent().trim());
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, e);
        }
        return lesDeps;
    }
}
